package com.example.networktest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;

public class networkingTest extends Activity
{
  private networkingTestService mNetworkService = null;
  private String blah = "blah";
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv = new TextView(this);
    tv.setText("Hello, Android\n");
    tv.setText("Starting Network Service"+blah);
    setContentView(tv);

    // Initialize Network Service, and determine ip
    if(mNetworkService == null)
      mNetworkService = new networkingTestService(this, mHandler);
    if( mNetworkService.refreshLocalIPAddress() == 1)
      tv.setText("No network connections detected\n");
    if( mNetworkService.refreshLocalIPAddress() == 0)
      tv.setText("This phone's IP address is "+mNetworkService.getLocalIPAddress());
  }

  // The Handler that gets information back from the networkSerive
  // NOTE: This was copied from the bluetooth example on the android
  // website.  I think that most of the communication between the
  // network interface and the application will be done through 
  // reading and writing to files so this handler might not be
  // necessary.  Currently, it does nothing.
  private final Handler mHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        // This was copied from the BlueToothChat program and is left here
        // as a template
          //switch (msg.what) {
          //case MESSAGE_STATE_CHANGE:
          //    if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
          //    switch (msg.arg1) {
          //    case BluetoothChatService.STATE_CONNECTED:
          //        mTitle.setText(R.string.title_connected_to);
          //        mTitle.append(mConnectedDeviceName);
          //        mConversationArrayAdapter.clear();
          //        break;
          //    case BluetoothChatService.STATE_CONNECTING:
          //        mTitle.setText(R.string.title_connecting);
          //        break;
          //    case BluetoothChatService.STATE_LISTEN:
          //    case BluetoothChatService.STATE_NONE:
          //        mTitle.setText(R.string.title_not_connected);
          //        break;
          //    }
          //    break;
          //case MESSAGE_WRITE:
          //    byte[] writeBuf = (byte[]) msg.obj;
          //    // construct a string from the buffer
          //    String writeMessage = new String(writeBuf);
          //    mConversationArrayAdapter.add("Me:  " + writeMessage);
          //    break;
          //case MESSAGE_READ:
          //    byte[] readBuf = (byte[]) msg.obj;
          //    // construct a string from the valid bytes in the buffer
          //    String readMessage = new String(readBuf, 0, msg.arg1);
          //    mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
          //    break;
          //case MESSAGE_DEVICE_NAME:
          //    // save the connected device's name
          //    mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
          //    Toast.makeText(getApplicationContext(), "Connected to "
          //                   + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
          //    break;
          //case MESSAGE_TOAST:
          //    Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
          //                   Toast.LENGTH_SHORT).show();
          //    break;
          //}
      }
  };

}
